// eslint-disable-next-line no-undef
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
// eslint-disable-next-line no-undef
const PurgecssPlugin = require('purgecss-webpack-plugin')
// eslint-disable-next-line no-undef
const glob = require('glob-all')
// eslint-disable-next-line no-undef
const path = require('path')
// eslint-disable-next-line no-undef
const resolve = dir => path.resolve(__dirname, dir)

/*
  配置：https://github.com/staven630/nuxt-config
*/
export default {
  // mode: 'spa',
  ssr: false,
  /*
   * Headers of the page
   */
  head: {
    // process.env.title ||
    // eslint-disable-next-line no-undef
    title: '施工图' || process.env.npm_package_name || '',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      // eslint-disable-next-line no-undef
      {
        hid: 'description',
        name: 'description',
        content: process.env.npm_package_description || ''
      }
    ],
    link: [{ rel: 'icon', type: 'image/x-icon', href: '/static/logo.png' }],
    script: [
      // 图标库 iconfont prototype项目 https://www.iconfont.cn/manage/index?manage_type=myprojects&projectId=1795976
      { src: '//at.alicdn.com/t/font_1795976_xwemb591s5h.js' }
    ]
  },
  /*
   ** Customize the progress-bar color
   */
  loading: { color: '#fff' },
  /*
   ** Global CSS
   */
  css: ['ant-design-vue/dist/antd.css', '@/assets/global.less'],
  /*
   ** Plugins to load before mounting the App
   */
  plugins: [
    // '@/plugins/init',
    '@/plugins/antd-ui',
    { src: '@/plugins/icon', ssr: false },
    { src: '@/plugins/hotkey', ssr: false }
  ],
  /*
   ** Nuxt.js dev-modules
   */
  buildModules: ['@nuxt/typescript-build'],
  /*
   ** Nuxt.js modules
   */
  modules: [
    // Doc: https://axios.nuxtjs.org/usage
    '@nuxtjs/axios',
    '@nuxtjs/pwa',
    // Doc: https://github.com/nuxt-community/dotenv-module
    '@nuxtjs/dotenv',
    // Doc https://blog.csdn.net/sinat_35538827/article/details/100122663
    '@nuxtjs/style-resources' // 添加对应的模块
  ],
  styleResources: {
    less: [
      './assets/global.less' // 全局 less 变量
    ]
  },
  /*
   ** Axios module configuration
   ** See https://axios.nuxtjs.org/options
   */
  axios: {},
  /*
   ** Build configuration
   */
  build: {
    /*
     ** You can extend webpack config here
     */
    extend(config, ctx) {
      // hard-source-webpack-plugin
      if (ctx.isDev) {
        config.plugins.push(
          new HardSourceWebpackPlugin({
            cacheDirectory: '.cache/hard-source/[confighash]'
          })
        )
      } // end hard-source-webpack-plugin

      // glob-all purgecss-webpack-plugin
      if (!ctx.isDev) {
        config.plugins.push(
          new PurgecssPlugin({
            paths: glob.sync([
              resolve('./pages/**/*.vue'),
              resolve('./layouts/**/*.vue'),
              resolve('./components/**/*.vue')
            ]),
            extractors: [
              {
                extractor: class Extractor {
                  static extract(content) {
                    const validSection = content.replace(
                      /<style([\s\S]*?)<\/style>+/gim,
                      ''
                    )
                    return validSection.match(/[A-Za-z0-9-_:/]+/g) || []
                  }
                },
                extensions: ['vue']
              }
            ],
            whitelist: ['html', 'body', 'nuxt-progress']
          })
        )
      } // end glob-all purgecss-webpack-plugin
    }
  }
}
